package com.lsnu.ehr.controller;

import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lsnu.ehr.domain.Pages;
import com.lsnu.ehr.domain.Result;
import com.lsnu.ehr.entity.Emp;
import com.lsnu.ehr.entity.Post;
import com.lsnu.ehr.jpa.EmpRepository;
import com.lsnu.ehr.jpa.PostRepository;
/**
 * 岗位控制器
 * @author 小七
 *
 */
//允许跨域请求
@CrossOrigin
// 以json形式返回
@ResponseBody
// 控制器
@Controller
public class PostController {
	private static final Integer pageLength = 5;
	// 依赖注入，类似于配置spring的bean
	@Autowired
	PostRepository repositories;
	@Autowired
	EmpRepository empRep;
	/**
	 * 添加岗位
	 * @param post
	 * @return
	 */
	@RequestMapping("addPost")
	public Object addPost(Post post) {
		
		// 使用接口直接存储部门，spring会自动代理接口方法時
		// 返回值是一个持久态的Post
		Post dbPost = repositories.save(post);
		
		boolean flag = (null != dbPost.getId());
		
		return new Result(1,flag);
	}
	
		/**
		 * 更新岗位
		 * @param post
		 * @return
		 */
		@RequestMapping("updatePost")
		public Object updatePost(Post post) {
			
			Boolean flag = false;
			
			if(repositories.findById(post.getId()) != null) {
				flag = true;
				repositories.save(post);
			}
			
			return new Result(1,flag);
		}
		
		/**
		 * 查询所有岗位
		 * @return
		 */
		@RequestMapping("findPost")
		public Object findPost() {
			
			// 使用接口直接存储部门，spring会自动代理接口方法時
			List<Post> posts = repositories.findAll();
			
			return new Result(1, posts);
		}
		
		@SuppressWarnings("deprecation")
		@RequestMapping("findPostByKey")
		public Object findPostByKey(@RequestParam("key") String key, 
				@RequestParam("page") Integer page) {
			Boolean flag = true;
			Page<Post> postPage = null;
			List<Post> posts = null;
			Integer count = 0;
			Order order = new Order(Direction.ASC, "id"); // 根据字段id进行排序
			// 分页排序查询
			// 判断是否全是数字
			Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
			flag = pattern.matcher(key).matches();  
			
			PageRequest pageRequest = new PageRequest(page - 1, pageLength, new Sort(order));
			System.out.println("Key:"+key);
			System.out.println("page:"+page);
			if("".equals(key)) {
				postPage = repositories.findAll(pageRequest);
				posts = postPage.getContent();
				count = (int)postPage.getTotalElements();
			}else if(flag) {
				count = 1;
				Optional<Post> opt = repositories.findById(new Long(key));
				return new Result(1,opt, new Pages(page, count, pageLength), "成功！");
			}else {
				postPage = repositories.findByPostName(pageRequest, key);
				posts = postPage.getContent();
				count = (int)postPage.getTotalElements();
			}
			
			return new Result(1, posts, new Pages(page, count, pageLength), "成功！");
		}
		
		/**
		 * 根据id删除岗位
		 * @param id
		 * @return
		 */
		@RequestMapping("delPost")
		public Object delPost(Long id) {
			
			List<Emp> emps = empRep.findEmpPostById(id);
			// 设置员工的部门为null--后期可进行数据处理
			for(Emp emp : emps) {
				emp.setEmpDiv(null);
			}
			repositories.deleteById(new Long(id));
			
			return new Result(1, true,"删除成功");
		}
		
}